package dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Persona;
import dao.Entidad;

public class MySQLPersonaDAO implements Entidad<Persona> {

	@Override
	public Persona insert(Persona t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO persona(id_persona,nombre,apellido_paterno,apellido_materno,direccion,celular,";
			sql = sql + "telefono,nacionalidad,id_pais,id_departamento,id_provincia,id_distrito) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getId_persona());
			ps.setString(2, t.getNombre());
			ps.setString(3, t.getApellido_paterno());
			ps.setString(4, t.getApellido_materno());
			ps.setString(5, t.getDireccion());
			ps.setInt(6, t.getCelular());
			ps.setInt(7, t.getTelefono());
			ps.setString(8, t.getNacionalidad());
			ps.setInt(9, t.getId_pais());
			ps.setInt(10, t.getId_departamento());
			ps.setInt(11, t.getId_provincia());
			ps.setInt(12, t.getId_distrito());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_persona(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public int update(Persona t) {
		/*Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE persona SET nombre=?,apellido_paterno=?,apellido_materno=?,";
			sql = sql +	"direccion=?,celular=?,telefono=?,nacionalidad=?,id_pais,id_departamento=?";
			sql = sql +	"id_provincia=?,id_distrito= ? WHERE id_persona=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, t.getNombre());
			ps.setString(2, t.getApellido_paterno());
			ps.setString(3, t.getApellido_materno());
			ps.setString(4, t.getDireccion());
			ps.setInt(5, t.getCelular());
			ps.setInt(6, t.getTelefono());
			ps.setString(7, t.getNacionalidad());
			ps.setInt(8, t.getId_pais());
			ps.setInt(9, t.getId_departamento());
			ps.setInt(10, t.getId_provincia());
			ps.setInt(11, t.getId_distrito());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public int delete(Persona t) {
		/*Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM persona WHERE id_persona=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_persona());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public Persona findById(Persona t) {
		// TODO Auto-generated method stub
			/*	Connection con=null;
				try {
					con=DataBaseConnection.openConnection();
					String sql="SELECT * FROM persona WHERE id_persona=?";
					PreparedStatement ps=con.prepareStatement(sql);
					ps.setInt(1, t.getId_persona());
					ResultSet rs=ps.executeQuery();
					while(rs.next()){
						t.setId_persona(rs.getInt("id_persona"));
						t.setNombre(rs.getString("nombre"));
						t.setApellido_paterno(rs.getString("apellido_paterno"));
						t.setApellido_materno(rs.getString("apellido_materno"));
						t.setDireccion(rs.getString("direccion"));
						t.setCelular(rs.getInt("celular"));		
						t.setTelefono(rs.getInt("telefono"));
						t.setNacionalidad(rs.getString("nacionalidad"));
						t.setId_pais(rs.getInt("id_pais"));
						t.setId_departamento(rs.getInt("id_departamento"));
						t.setId_provincia(rs.getInt("id_provincia"));
						t.setId_distrito(rs.getInt("id_distrito"));
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					DataBaseConnection.closeConnection(con);
				}*/
				return null;
	}

	@Override
	public List<Persona> listAll() {
		// TODO Auto-generated method stub
				/*List<Persona> personas = new ArrayList<Persona>();
				Connection con=null;
				try {
					con=DataBaseConnection.openConnection();
					String sql="SELECT * FROM persona";
					Statement st=con.createStatement();
					ResultSet rs=st.executeQuery(sql);
					while(rs.next()){
						Persona t = new Persona();
						t.setId_persona(rs.getInt("id_persona"));
						t.setNombre(rs.getString("nombre"));
						t.setApellido_paterno(rs.getString("apellido_paterno"));
						t.setApellido_materno(rs.getString("apellido_materno"));
						t.setDireccion(rs.getString("direccion"));
						t.setCelular(rs.getInt("celular"));		
						t.setTelefono(rs.getInt("telefono"));
						t.setNacionalidad(rs.getString("nacionalidad"));
						t.setId_pais(rs.getInt("id_pais"));
						t.setId_departamento(rs.getInt("id_departamento"));
						t.setId_provincia(rs.getInt("id_provincia"));
						t.setId_distrito(rs.getInt("id_distrito"));	
						personas.add(t);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					DataBaseConnection.closeConnection(con);
				}*/
				return null;
	}

	@Override
	public List<Persona> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
